import React from 'react'
import { inject, observer } from 'mobx-react';
import { Bubble, Tools} from 'weaver-mobile'
import { RouteLayout, Form } from 'weaver-mobile-page'
import { withRouter } from 'react-router-dom'
const ButtonGroup = RouteLayout.ButtonGroup;
const getLabel = Tools.getLabel;
@inject('wfAgentStore')
@withRouter
@observer
export default class SaveAsTemplate extends React.Component {
    componentWillMount() {
        const { location, wfAgentStore } = this.props;
        const { getPageTitle } = wfAgentStore;
        const { pathname = "" } = location;
        getPageTitle(pathname);
    }
    componentDidMount() {
        let items = [{
            "colSpan": 2,
            "conditionType": "INPUT",
            "domkey": ["templatename"],
            "fieldcol": 12,
            "isQuickSearch": true,
            "label": getLabel(18151,"模板名称"),
            "labelcol": 6,
            "value": "",
            "viewAttr": 3
        }]
        const { SaveAsTem } = this.props.wfAgentStore;
        const { form } = SaveAsTem;
        form.initForm(items, '/agent/saveAsTemplate');
    }
    componentWillUpdate() {
        const { location, wfAgentStore } = this.props;
        const { getPageTitle } = wfAgentStore;
        const { pathname = "" } = location;
        getPageTitle(pathname);
    }
    changeBoolToNum(val) {
        if (val === true) {
            val = "1";
        } else {
            val = "0";
        }
        return val;
    }

    render() {
        const { form } = this.props.wfAgentStore.SaveAsTem;
        return (<div className='wf_addAgent'>
            <Form store={form} />
            <div style={{ width: "100%", position: "absolute", bottom: "0" }}>
                <ButtonGroup
                    data={[{
                        key: 'save',
                        text: getLabel(86,"保存"),
                        onClick: () => {
                            const { saveAgentTemplate, SaveAsTem } = this.props.wfAgentStore;
                            const { form } = SaveAsTem;
                            let templatename = form.formParams.templatename;
                            if (templatename === "") {
                                Bubble.warning(getLabel(18510,'请输入模板名称'), 2);
                            } else {
                                let params = {
                                    templatename: templatename
                                }
                                const { addAgent } = this.props.wfAgentStore;
                                const { form } = addAgent;
                                params = { ...form.formParams, ...params }
                                params.isPendThing = this.changeBoolToNum(params.isPendThing)
                                params.isCreateAgenter = this.changeBoolToNum(params.isCreateAgenter)
                                params.isSysCreateAgenter = this.changeBoolToNum(params.isSysCreateAgenter)
                                params.isProxyDeal = this.changeBoolToNum(params.isProxyDeal)
                                params.method = "add"
                                if (params.templatename.length > 200) {
                                    Bubble.warning(getLabel(391146,"模板名称长度不能超过200字符"), 2)
                                } else {
                                    saveAgentTemplate(params, () => {
                                        this.props.history.go(-1)
                                    })
                                }
                            }
                        },
                        type: 'primary',
                    }]}
                />
            </div>

        </div>)
    }
}